package com.zjj.lbw.designpattern.builder;

/**
 * @author zhanglei.zjj
 * @description 标准建造者模式
 * @date 2023/7/12 17:27
 */
public class BuilderV1Test {
    public static void main(String[] args) {

        ProductBuilder productBuilder = new DefaultProductBuilder();

        Director director = new Director(productBuilder);
        Product product = director.makeProduct("part1", "part2", "part3", "part4");
        System.out.println(product);
    }
}

/**
 * 抽象建造者
 * 描述构建复杂对象需要创建那些部分以及把这些部分组装在一起的规范
 */
interface ProductBuilder {

    void buildPart1(String part1);

    void buildPart2(String part2);

    void buildPart3(String part3);

    void buildPart4(String part4);

    Product build();
}

/**
 * 具体建造者
 */
class DefaultProductBuilder implements ProductBuilder {

    private String part1;
    private String part2;
    private String part3;
    private String part4;

    @Override
    public void buildPart1(String part1) {
        this.part1 = part1;
    }

    @Override
    public void buildPart2(String part2) {
        this.part2 = part2;
    }

    @Override
    public void buildPart3(String part3) {
        this.part3 = part3;
    }

    @Override
    public void buildPart4(String part4) {
        this.part4 = part4;
    }

    @Override
    public Product build() {
        return new Product(part1, part2, part3, part4);
    }
}

/**
 * 指挥者
 */
class Director {

    private ProductBuilder productBuilder;

    public Director(ProductBuilder productBuilder) {
        this.productBuilder = productBuilder;
    }

    public Product makeProduct(String part1, String part2, String part3, String part4) {
        productBuilder.buildPart1(part1);
        productBuilder.buildPart2(part2);
        productBuilder.buildPart3(part3);
        productBuilder.buildPart4(part4);
        Product product = productBuilder.build();
        return product;

    }

}

/**
 * 产品
 */
class Product {


    private String part1;
    private String part2;
    private String part3;
    private String part4;
    //  .....


    public Product(String part1, String part2, String part3, String part4) {
        this.part1 = part1;
        this.part2 = part2;
        this.part3 = part3;
        this.part4 = part4;
    }

    public String getPart1() {
        return part1;
    }

    public void setPart1(String part1) {
        this.part1 = part1;
    }

    public String getPart2() {
        return part2;
    }

    public void setPart2(String part2) {
        this.part2 = part2;
    }

    public String getPart3() {
        return part3;
    }

    public void setPart3(String part3) {
        this.part3 = part3;
    }

    public String getPart4() {
        return part4;
    }

    public void setPart4(String part4) {
        this.part4 = part4;
    }

    @Override
    public String toString() {
        return "Product{" +
                "part1='" + part1 + '\'' +
                ", part2='" + part2 + '\'' +
                ", part3='" + part3 + '\'' +
                ", part4='" + part4 + '\'' +
                '}';
    }
}